package com.project_test.kun.travelling_1st.toolActivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;

import com.project_test.kun.travelling_1st.R;
import com.project_test.kun.travelling_1st.beans.GlobalCity;

import java.util.ArrayList;
import java.util.List;

public class CityChooseActivity extends AppCompatActivity implements View.OnClickListener {
    private String[] CityNameList;
    private int[] CityIdList;
    private int[] CityImgIdList;
    private List<CityInfo> cities;
    private List<CityInfo> citiesCopy;
    private GridView cityGrid;
    private EditText city_choose_edit;
    private ImageView city_choose_finish,city_choose_delete;
//    private GlobalCity city;
    private ExtendedWatcher watcher;
    private GAdapter gAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_choose);
//        city=GlobalCity.getInstance();
        cities=new ArrayList<>();
        citiesCopy=new ArrayList<>();
        initView();
                gAdapter=new GAdapter(cities);
        cityGrid.setAdapter(gAdapter);
        initCityList();
        gAdapter.notifyDataSetChanged();
        city_choose_finish.setOnClickListener(this);
        city_choose_delete.setClickable(true);
        watcher=new ExtendedWatcher();
        city_choose_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                city_choose_edit.setText("");
            }
        });

        city_choose_edit.addTextChangedListener(watcher);


    }

    private void initView() {
        cityGrid= (GridView) findViewById(R.id.city_grid);
        city_choose_delete= (ImageView) findViewById(R.id.city_choose_city_delete);
        city_choose_finish= (ImageView) findViewById(R.id.city_choose_finish);
        city_choose_edit= (EditText) findViewById(R.id.city_choose_edit);
    }

    private void initCityList() {
       CityNameList=new String[]{"北京","东京都","大阪府","北海道","冲绳","京都府","神奈川","群马县","岐阜县","爱知县","兵库县"};
        CityIdList=new int[]{143,144,145,146,148,147,150,153,163,166,160};
        CityImgIdList=new int[]{R.mipmap.city_img_0,R.mipmap.city_img_1,R.mipmap.city_img_2,R.mipmap.city_img_3,R.mipmap.city_img_4,R.mipmap.city_img_5,
                R.mipmap.city_img_6,R.mipmap.city_img_7,R.mipmap.city_img_8,R.mipmap.city_img_9,R.mipmap.city_img_10};
        for (int i = 0; i < 11; i++) {
            CityInfo city=new CityInfo(CityIdList[i],CityImgIdList[i],CityNameList[i]);
            cities.add(city);
            citiesCopy.add(city);
        }
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    class GAdapter extends BaseAdapter{
        private List<CityInfo> mCities;

        public GAdapter(List<CityInfo> cities) {
            mCities=cities;
        }

        @Override
        public int getCount() {
            return mCities==null?0:mCities.size();
        }

        @Override
        public Object getItem(int position) {
            return mCities.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View ret=convertView;
            LayoutInflater inflater=LayoutInflater.from(getBaseContext());
            ret=inflater.inflate(R.layout.city_choose_grid_item,parent,false);
            Holder holder=(Holder)ret.getTag();
            if (holder==null){
            holder=new Holder(ret);
            ret.setTag(holder);
            }
            holder.bindView(position,mCities.get(position));
            return ret;
        }

        private class Holder{
            private ImageView city_choose_city_img;

            public Holder(View itemView) {
                city_choose_city_img = (ImageView) itemView.findViewById(R.id.city_choose_city_img);
            }
            public void bindView(final int i, CityInfo item) {
                city_choose_city_img.setImageResource(item.getCityImgId());
                city_choose_city_img.setClickable(true);
                city_choose_city_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CityInfo cityChosen=mCities.get(i);
                        GlobalCity.getInstance().setGlobalCityId(cityChosen.getCtiyId());
                        GlobalCity.getInstance().setGlobalCityName(cityChosen.getCityName());
                        finish();
                    }
                });

            }
        }
    }


    private class CityInfo{
        private int CtiyId;
        private int CityImgId;
        private String CityName;

        public CityInfo(int id,int imgid,String name){
            this.CtiyId=id;
            this.CityImgId=imgid;
            this.CityName=name;
        };

        public int getCtiyId() {
            return CtiyId;
        }

        public int getCityImgId() {
            return CityImgId;
        }

        public String getCityName() {
            return CityName;
        }
    }

    class ExtendedWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(count==0){
                cities.clear();
                cities.addAll(citiesCopy);
                gAdapter.notifyDataSetChanged();
            }

            if (count==1||count==2||count==3){
                cities.clear();
             switch (count){
                 case 1:
                   String compareTmp=city_choose_edit.getText().toString();
                     for (int i = 0; i < citiesCopy.size(); i++) {
                         String nameTmp=citiesCopy.get(i).getCityName().toString();
                         String compareTarget=nameTmp.substring(0,1);
                         if (compareTmp.equals(compareTarget)){
                             cities.add(citiesCopy.get(i));
                         }
                     }
                     break;
                 case 2:

                     String compareTmp2=city_choose_edit.getText().toString();
                     for (int i = 0; i < citiesCopy.size(); i++) {
                         String nameTmp2=citiesCopy.get(i).getCityName().toString();
                         String compareTarget2=nameTmp2.substring(0,2);
                         if (compareTmp2.equals(compareTarget2)){
                             cities.add(citiesCopy.get(i));
                         }
                     }
                     break;
                 case 3:
                     String compareTmp3=city_choose_edit.getText().toString();
                     for (int i = 0; i < citiesCopy.size(); i++) {
                         String nameTmp3=citiesCopy.get(i).getCityName().toString();
                        if (nameTmp3.length()>=3){
                         String compareTarget3=nameTmp3.substring(0,3);
                         if (compareTmp3.equals(compareTarget3)){
                             cities.add(citiesCopy.get(i));}
                         }
                     }
                     break;
             }
            gAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
